//
//  EveryExpress.m
//  TengyunStation
//
//  Created by Johnny on 15/8/31.
//  Copyright (c) 2015年 xingrong. All rights reserved.
//

#import "EveryExpress.h"
#import "SocketMethod.h"

@implementation EveryExpress

- (id)initWithString:(NSString *)string {
    self = [super init];
    if (self) {
        NSArray *array = [string componentsSeparatedByString:@","];
        self.expressCompanyId = [SocketMethod subArray:array Index:0];
        self.expressCompanyName = [SocketMethod subArray:array Index:1];
        self.expressId = [SocketMethod subArray:array Index:2];
        self.inwayExpressId = [SocketMethod subArray:array Index:3];
        self.telephone = [SocketMethod subArray:array Index:4];
        self.expressType = [SocketMethod subArray:array Index:5];
        self.smsSendType = [[SocketMethod subArray:array Index:6] intValue];
        NSString *receiveTime = [SocketMethod subArray:array Index:7];
        [self handleReceiveTime:receiveTime];
        self.isOverdue = [[SocketMethod subArray:array Index:8] intValue];
        self.remark = [SocketMethod subArray:array Index:9];
        self.expressCompanyLogoUrl = [SocketMethod subArray:array Index:10];
        self.dayCount = [[SocketMethod subArray:array Index:11] intValue];
    }
    return self;
}

- (void)handleReceiveTime:(NSString *)receiveTime {
    self.receiveTime = [SocketMethod dateStringWithDateString:receiveTime];
    if (receiveTime.length >= 8) {
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        dateFormatter.dateFormat = @"yyyyMMdd";
        NSDate *date = [dateFormatter dateFromString:[receiveTime substringToIndex:8]];
        NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
        NSDateComponents *componets = [calendar components:NSDayCalendarUnit fromDate:date toDate:[NSDate date] options:NSCalendarWrapComponents];
        if (componets.day <= 0) {
            self.showDay = @"今天";
        } else if (componets.day <= 1) {
            self.showDay = @"昨天";
        } else {
            NSDateFormatter *showFormatter = [[NSDateFormatter alloc] init];
            showFormatter.dateFormat = @"MM月dd日";
            self.showDay = [showFormatter stringFromDate:date];
        }
    }
}

@end
